package com.example.eat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.eat.R;
import com.example.eat.dao.RecipeDao;
import com.example.eat.model.Recipe;
import com.squareup.picasso.Picasso;
import java.util.List;

public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.ViewHolder> {
    private Context context;
    private List<Recipe> recipeList;
    private OnItemClickListener listener;
    private int currentUserId; // 当前登录用户ID
    private RecipeDao recipeDao;

    public interface OnItemClickListener {
        void onItemClick(int position);
        void onEditClick(int position);
        void onDeleteClick(int position);
        void onFavoriteClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public RecipeAdapter(Context context, List<Recipe> recipeList, int currentUserId) {
        this.context = context;
        this.recipeList = recipeList;
        this.currentUserId = currentUserId;
        this.recipeDao = new RecipeDao(context);
    }

    public void setRecipes(List<Recipe> recipes) {
        this.recipeList = recipes;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recipe, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Recipe recipe = recipeList.get(position);

        holder.tvName.setText(recipe.getName());
        holder.tvCategory.setText(recipe.getCategory());

        // 加载图片
        if (recipe.getImage() != null && !recipe.getImage().isEmpty()) {
            Picasso.get()
                    .load(recipe.getImage())
                    .placeholder(R.drawable.ic_recipe_placeholder)
                    .into(holder.ivRecipe);
        } else {
            holder.ivRecipe.setImageResource(R.drawable.ic_recipe_placeholder);
        }

        // 设置收藏按钮状态
        if (recipe.isFavorite()) {
            holder.btnFavorite.setImageResource(R.drawable.ic_favorite);
        } else {
            holder.btnFavorite.setImageResource(R.drawable.ic_favorite_border);
        }

        // 设置编辑和删除按钮的可见性
        boolean isMyRecipe = recipe.getUserId() == currentUserId;

        if (isMyRecipe) {
            holder.btnEdit.setVisibility(View.VISIBLE);
            holder.btnDelete.setVisibility(View.VISIBLE);
        } else {
            holder.btnEdit.setVisibility(View.GONE);
            holder.btnDelete.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return recipeList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivRecipe;
        TextView tvName, tvCategory;
        ImageButton btnEdit, btnDelete, btnFavorite;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            ivRecipe = itemView.findViewById(R.id.imageViewRecipe);
            tvName = itemView.findViewById(R.id.textViewName);
            tvCategory = itemView.findViewById(R.id.textViewCategory);
            btnEdit = itemView.findViewById(R.id.imageButtonEdit);
            btnDelete = itemView.findViewById(R.id.imageButtonDelete);
            btnFavorite = itemView.findViewById(R.id.imageButtonFavorite);

            // 设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onItemClick(position);
                        }
                    }
                }
            });

            btnEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onEditClick(position);
                        }
                    }
                }
            });

            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onDeleteClick(position);
                        }
                    }
                }
            });

            btnFavorite.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onFavoriteClick(position);
                        }
                    }
                }
            });
        }
    }
}